package cn.xingzhe.monster.user.infrastructure.persistence.converter;

import cn.xingzhe.monster.user.domain.model.RoleModel;
import cn.xingzhe.monster.user.infrastructure.persistence.entity.RoleEntity;

import java.util.ArrayList;

/**
 * 角色实体转换器
 * 负责 RoleEntity 和 RoleModel 之间的转换
 *
 * @author System
 * @version 1.0.0
 * @since 2024-01-01
 */
public class RoleEntityConverter {

    /**
     * Entity 转 Model
     *
     * @param entity 实体
     * @return 领域模型
     */
    public static RoleModel toModel(RoleEntity entity) {
        if (entity == null) {
            return null;
        }
        return RoleModel.builder()
                .id(entity.getId())
                .roleCode(entity.getRoleCode())
                .roleName(entity.getRoleName())
                .description(entity.getDescription())
                .permissionIds(entity.getPermissionIds() != null ? new ArrayList<>(entity.getPermissionIds()) : new ArrayList<>())
                .createTime(entity.getCreateTime())
                .updateTime(entity.getUpdateTime())
                .build();
    }

    /**
     * Model 转 Entity
     *
     * @param model 领域模型
     * @return 实体
     */
    public static RoleEntity toEntity(RoleModel model) {
        if (model == null) {
            return null;
        }
        return RoleEntity.builder()
                .id(model.getId())
                .roleCode(model.getRoleCode())
                .roleName(model.getRoleName())
                .description(model.getDescription())
                .permissionIds(model.getPermissionIds() != null ? new ArrayList<>(model.getPermissionIds()) : new ArrayList<>())
                .createTime(model.getCreateTime())
                .updateTime(model.getUpdateTime())
                .build();
    }
}

